<template>
  <view class="filter-item">
    <text class="font-item-text">子女情况</text>
    <view class="uni-input" @click="togglePopup">
      <template v-if="selectedChildStatus.length > 0">
        <text class="range-text">{{ selectedChildStatus.join(', ') }}</text>
      </template>
      <template v-else>请选择子女情况</template>
      <uni-icons :type="iconType" size="20" class="icon"></uni-icons>
    </view>
  </view>

  <view v-show="showPopupContent" class="popup-content">
    <view class="button-container">
      <button
        v-for="status in childStatusOptions"
        :key="status"
        :class="
          selectedChildStatus.includes(status) ||
          (status == '不限' && selectedChildStatus.length == 0)
            ? 'custom-button selected'
            : 'checkbox-label'
        "
        @click="() => changeChildStatus(status)"
      >
        {{ status }}
        <view
          v-if="
            selectedChildStatus.includes(status) ||
            (status == '不限' && selectedChildStatus.length == 0)
          "
          class="checkmark"
        ></view>
      </button>
    </view>
  </view>
</template>

<script setup lang="ts">
import { ref, defineEmits, defineProps, watch } from 'vue'

const props = defineProps<{
  childStatus: string[]
}>()

const emit = defineEmits<{
  (e: 'update:childStatus', value: string[]): void
}>()

const childStatusOptions = ['不限', '未育', '子女归自己', '子女归对方']
const selectedChildStatus = ref<string[]>([...props.childStatus]) // 默认选择不限
const showPopupContent = ref(false)
const iconType = ref('arrowright')

watch(
  () => props.childStatus,
  (newVal) => {
    selectedChildStatus.value = [...newVal]
  },
)

const togglePopup = () => {
  showPopupContent.value = !showPopupContent.value
  iconType.value = showPopupContent.value ? 'up' : 'arrowright'
}

const changeChildStatus = (status: string) => {
  if (status == '不限') {
    selectedChildStatus.value = []
  } else {
    let idx = selectedChildStatus.value.indexOf(status)
    if (idx == -1) {
      selectedChildStatus.value.push(status)
    } else {
      selectedChildStatus.value.splice(idx, 1)
    }
  }
  emit('update:childStatus', selectedChildStatus.value)
}
</script>

<style scoped>
.filter-item {
  font-size: 13px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  cursor: pointer;
  margin-bottom: 20px;
}
.font-item-text {
  color: #a9a5a3;
}

.uni-input {
  display: flex;
  align-items: center;
}

.range-text {
  color: #1e90ff;
}

.icon {
  color: #ccc;
  margin-left: 10px;
}

.popup-content {
  padding: 20px;
  background-color: #fff;
  width: 100%;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  z-index: 1000;
  margin-top: 10px;
}

.button-container {
  display: flex;
  flex-wrap: wrap;
  gap: 4px; /* 按钮之间的间距 */
}

.checkbox-label,
.custom-button {
  display: flex;
  justify-content: center;
  align-items: center;
  width: calc(33.3333% - 4px); /* 每行三个按钮，并考虑间距 */
  height: 40px;
  border-radius: 4px;
  font-size: 14px;
  position: relative;
  cursor: pointer;
  user-select: none;
  transition: all 0.3s;
  margin: 2px 0; /* 上下间距为2px，总间距为4px */
}

.checkbox-label {
  border: 1px solid #ff0000;
  background-color: #f5f5f5;
  color: #666;
}

.custom-button {
  border: 1px solid #00a0e9;
  color: #00a0e9; /* 文字颜色 */
  background-color: #e5f7f9; /* 背景颜色 */
}

.custom-button.selected::before {
  content: '';
  position: absolute;
  right: 0;
  bottom: 0;
  width: 0;
  height: 0;
  border-style: solid;
  border-width: 16px 12px 11px 10px;
  border-color: transparent rgb(1, 170, 190) rgb(1, 170, 190) transparent;
}

.custom-button.selected .checkmark::before {
  content: '✔';
  color: white;
  font-size: 16px;
  position: absolute;
  top: 50%;
  left: 63%;
  transform: translate(-50%, -50%);
  z-index: 1;
}

.custom-button .checkmark {
  position: absolute;
  bottom: 2px;
  right: 2px;
  width: 14px;
  height: 14px;
  border-radius: 2px;
  text-align: center;
  line-height: 14px;
}
</style>
